home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.AbstractButton;
- import com.sun.java.swing.ButtonModel;
- import com.sun.java.swing.Icon;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.SwingUtilities;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.border.Border;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.ToggleButtonUI;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.Rectangle;
- import java.io.Serializable;
-
- public class BasicToggleButtonUI extends ToggleButtonUI implements Serializable {
- private static ToggleButtonUI toggleButtonUI;
- private static BasicButtonListener listener;
- private static final int defaultTextIconGap = 4;
- private static final Insets defaultMargin = new Insets(2, 2, 2, 2);
- protected static Color toggleButtonFocus;
-
- protected BasicButtonListener createListener(JComponent c) {
- return new BasicButtonListener((AbstractButton)c);
- }
-
- public static ComponentUI createUI(JComponent b) {
- if (toggleButtonUI == null) {
- toggleButtonUI = new BasicToggleButtonUI();
- }
-
- return toggleButtonUI;
- }
-
- public Border getDefaultBorder(AbstractButton b) {
- return UIManager.getBorder("ToggleButton.border");
- }
-
- public Insets getDefaultMargin(AbstractButton b) {
- return defaultMargin;
- }
-
- public int getDefaultTextIconGap(AbstractButton b) {
- return 4;
- }
-
- public Dimension getPreferredSize(JComponent c) {
- AbstractButton b = (AbstractButton)c;
- return BasicGraphicsUtils.getPreferredButtonSize(b, 4);
- }
-
- protected void installDefaults(JComponent c) {
- LookAndFeel.installColorsAndFont(c, "ToggleButton.background", "ToggleButton.foreground", "ToggleButton.font");
- LookAndFeel.installBorder(c, "ToggleButton.border");
- toggleButtonFocus = UIManager.getColor("ToggleButton.focus");
- }
-
- protected void installKeyboardActions(JComponent c) {
- listener.setupKeyboard((AbstractButton)c);
- }
-
- protected void installListeners(JComponent c) {
- if ((listener = this.createListener(c)) != null) {
- ((Component)c).addMouseListener(listener);
- ((Component)c).addMouseMotionListener(listener);
- ((Component)c).addFocusListener(listener);
- ((AbstractButton)c).addChangeListener(listener);
- }
-
- }
-
- public void installUI(JComponent c) {
- this.installDefaults(c);
- this.installListeners(c);
- this.installKeyboardActions(c);
- }
-
- public void paint(Graphics g, JComponent c) {
- AbstractButton b = (AbstractButton)c;
- ButtonModel model = b.getModel();
- Dimension size = ((Component)b).getSize();
- FontMetrics fm = g.getFontMetrics();
- Insets i = c.getInsets();
- Rectangle viewRect = new Rectangle(size);
- viewRect.x += i.left;
- viewRect.y += i.top;
- viewRect.width -= i.right + viewRect.x;
- viewRect.height -= i.bottom + viewRect.y;
- Rectangle iconRect = new Rectangle();
- Rectangle textRect = new Rectangle();
- Font f = ((Component)c).getFont();
- g.setFont(f);
- String text = SwingUtilities.layoutCompoundLabel(fm, b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, b.getText() == null ? 0 : 4);
- g.setColor(((Component)b).getBackground());
- if (model.isArmed() && model.isPressed() || model.isSelected()) {
- this.paintButtonPressed(g, b);
- }
-
- if (b.getIcon() != null) {
- this.paintIcon(g, c, iconRect);
- }
-
- if (text != null && !text.equals("")) {
- this.paintText(g, c, textRect, text);
- }
-
- if (b.isFocusPainted() && ((JComponent)b).hasFocus()) {
- this.paintFocus(g, b, viewRect, textRect, iconRect);
- }
-
- }
-
- protected void paintButtonPressed(Graphics g, AbstractButton b) {
- }
-
- protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
- }
-
- protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
- AbstractButton b = (AbstractButton)c;
- ButtonModel model = b.getModel();
- Icon icon = null;
- if (!model.isEnabled()) {
- icon = b.getDisabledIcon();
- } else if (model.isPressed() && model.isArmed()) {
- icon = b.getPressedIcon();
- if (icon == null) {
- icon = b.getSelectedIcon();
- }
- } else if (model.isSelected()) {
- icon = b.getSelectedIcon();
- } else if (b.isRolloverEnabled() && model.isRollover()) {
- icon = b.getRolloverIcon();
- }
-
- if (icon == null) {
- icon = b.getIcon();
- }
-
- icon.paintIcon(c, g, iconRect.x, iconRect.y);
- }
-
- protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
- AbstractButton b = (AbstractButton)c;
- ButtonModel model = b.getModel();
- FontMetrics fm = g.getFontMetrics();
- if (model.isEnabled()) {
- g.setColor(((Component)b).getForeground());
- BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
- } else {
- g.setColor(((Component)b).getBackground().brighter());
- BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x, textRect.y + fm.getAscent());
- g.setColor(((Component)b).getBackground().darker());
- BasicGraphicsUtils.drawString(g, text, model.getMnemonic(), textRect.x - 1, textRect.y + fm.getAscent() - 1);
- }
-
- }
-
- protected void uninstallDefaults(JComponent c) {
- LookAndFeel.uninstallBorder(c);
- }
-
- protected void uninstallKeyboardActions(JComponent c) {
- c.resetKeyboardActions();
- }
-
- protected void uninstallListeners(JComponent c) {
- ((Component)c).removeMouseListener(listener);
- ((Component)c).removeMouseMotionListener(listener);
- ((Component)c).removeFocusListener(listener);
- ((AbstractButton)c).removeChangeListener(listener);
- }
-
- public void uninstallUI(JComponent c) {
- this.uninstallKeyboardActions(c);
- this.uninstallListeners(c);
- this.uninstallDefaults(c);
- }
- }
-